//
//  AliMNSAccount.m
//  AlibabaCloudMNSiOS
//
//  Created by David Chen on 28/03/2017.
//  Copyright © 2017 AliCloud. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "AliMNSAccount.h"

@implementation AliMNSAccount
@synthesize mnsEndpoint;
@synthesize mnsAccessId;
@synthesize mnsAccessKey;
@synthesize mnsSecurityToken;
@synthesize mnsClient;

- (instancetype) initWithEndpoint:(NSString*) endpoint
                     withAccessId:(NSString*) accessId
                    withAccessKey:(NSString*) accessKey
{
    return [self initWithEndpoint:endpoint withAccessId: accessId withAccessKey: accessKey withSecurityToken:nil];
}

-(instancetype)initWithEndpoint:(NSString*)endpoint
               withAccessId:(NSString*)accessId
               withAccessKey:(NSString*)accessKey
               withSecurityToken:(NSString*)securityToken
{
    mnsEndpoint = endpoint;
    mnsAccessId = accessId;
    mnsAccessKey = accessKey;
    mnsSecurityToken = securityToken;
    mnsClient = [[AliMNSClient alloc] initWithHost:endpoint withAccessId:accessId withAccessKey:accessKey withVersion:@"2015-06-06" withSecurityToken:securityToken];
    return self;
}

-(AliMNSQueue*) getQueue:(NSString *)queueName
{
    return [[AliMNSQueue alloc] initWithQueueName:queueName withMNSClient:mnsClient];
}

-(AliMNSTopic*) getTopic:(NSString *)topicName
{
    return [[AliMNSTopic alloc] initWithTopicName:topicName withMNSClient:mnsClient];
}

-(AliMNSClient*) getMNSClient
{
    return mnsClient;
}

-(NSArray*) listQueue:(NSString*) prefix
     withReturnNumber:(NSInteger)returnNumber
           withMarker:(NSString**) marker;
{
    AliMNSListQueueRequest* lqreq = [[AliMNSListQueueRequest alloc] initWithPrefix:prefix withReturnNumber:returnNumber withMarker:*marker];
    AliMNSListQueueResponse* lqres = [AliMNSListQueueResponse new];

    [mnsClient sendRequest : lqreq withResponse: lqres];
    *marker = [lqres.nextMarker copy];
    return [lqres.queueMetaList copy];
}

-(NSArray*) listTopic:(NSString*) prefix
     withReturnNumber:(NSInteger)returnNumber
           withMarker:(NSString**) marker
{
    AliMNSListTopicRequest* ltReq = [[AliMNSListTopicRequest alloc] initWithPrefix:prefix withReturnNumber:returnNumber withMarker: *marker];
    AliMNSListTopicResponse* ltRes = [AliMNSListTopicResponse new];
    [mnsClient sendRequest:ltReq withResponse:ltRes];
    *marker = [ltRes.nextMarker copy];
    return [ltRes.topicList copy];
}

/*This api is design to use STS on mobile app. Refer to https://help.aliyun.com/document_detail/28657.html for more detail about STS*/
-(void) updateTempAK:(NSString*) pAccessId
       withAccessKey:(NSString*) pAccessKey
    withSecuriyToken:(NSString*) pSecurityToken
{
    mnsAccessId = pAccessId;
    mnsAccessKey = pAccessKey;
    mnsSecurityToken = pSecurityToken;

    mnsClient.accessId = pAccessId;
    mnsClient.accessKey = pAccessKey;
    mnsClient.securityToken = mnsSecurityToken;
}

@end
